<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * generate registry
     * @return Zend_Registry
     */
    protected function _initRegistry(){
        $registry = Zend_Registry::getInstance();
        return $registry;
    }
 

    protected function _initAutoload()
    {
        // Ensure front controller instance is present
        $this->bootstrap('frontController');
        $this->_frontController = $this->getResource('frontController');
        // Add autoloader empty namespace
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '', 
            'basePath' => APPLICATION_PATH,

            ));

        return $moduleLoader;
    }
    /**
     * Bootstrap Twig view
     */
    protected function _initView()
    {
        //init twig
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('Twig');
        $autoloader->registerNamespace('Zwig');
        $autoloader->registerNamespace('Dmd_');
//        $autoloader->suppressNotFoundWarnings(true);

        $view = new Zwig_View(array(
            'encoding' => 'UTF-8',
            'helperPath' => array(
                /* ... */
            ),
        ));

        $loader = new Twig_Loader_Filesystem(array());
        $zwig = new Zwig_Environment($view, $loader, array(
            'cache' => APPLICATION_PATH . '/../var/cache/twig/',
            'auto_reload' => true,
        ));

        $view->setEngine($zwig);
        $view->doctype(Zend_View_Helper_Doctype::XHTML1_STRICT);

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view, array(
            'viewSuffix' => 'twig',
        ));
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

        return $view;

    }

    /**
     * Initialize Doctrine
     * @return Doctrine_Manager
     */
    public function _initDoctrine() {
        // include and register Doctrine's class loader
        require_once('Doctrine/Common/ClassLoader.php');
        $classLoader = new \Doctrine\Common\ClassLoader(
            'Doctrine',
            APPLICATION_PATH . '/../libraries/'
        );
        $classLoader->register();

        // create the Doctrine configuration
        $config = new \Doctrine\ORM\Configuration();

        // setting the cache ( to ArrayCache. Take a look at
        // the Doctrine manual for different options ! )
        $cache = new \Doctrine\Common\Cache\ArrayCache;
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        // choosing the driver for our database schema
        // we'll use annotations
        $driver = $config->newDefaultAnnotationDriver(
            APPLICATION_PATH . '/models'
        );
        $config->setMetadataDriverImpl($driver);

        // set the proxy dir and set some options
        $config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
        $config->setAutoGenerateProxyClasses(true);
        $config->setProxyNamespace('App\Proxies');

        // now create the entity manager and use the connection
        // settings we defined in our application.ini
        $connectionSettings = $this->getOption('doctrine');
        $conn = array(
            'driver'    => $connectionSettings['conn']['driv'],
            'user'      => $connectionSettings['conn']['user'],
            'password'  => $connectionSettings['conn']['pass'],
            'dbname'    => $connectionSettings['conn']['dbname'],
            'host'      => $connectionSettings['conn']['host']
        );
        $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);

        // push the entity manager into our registry for later use
        $registry = Zend_Registry::getInstance();
        $registry->entitymanager = $entityManager;

        return $entityManager;
    }

    protected function _initAutoloader()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();

        require_once 'Doctrine/Common/ClassLoader.php';

        $entitiesAutoloader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models');
        $autoloader->pushAutoloader(array($entitiesAutoloader, 'loadClass'), 'Entities');

        $repositoriesAutoloader = new \Doctrine\Common\ClassLoader('Repositories', APPLICATION_PATH . '/models');
        $autoloader->pushAutoloader(array($repositoriesAutoloader, 'loadClass'), 'Repositories');

        return $autoloader;
    }

    protected function _initPlugin()
    {
        $plugin = new Dmd_Controller_Plugin_Acl(new Dmd_Acl);
        
        Zend_Controller_Front::getInstance()->registerPlugin($plugin);
    }
}